package com.sneakxy.cloudbase.platform.utils.web;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;

public class HttpServletRequestUtils {

	public static final String HEADER_X_PORT = "x-forwarded-port";
	
	public static final String HEADER_X_HOST = "x-forwarded-host";
	
	public static final String HEADER_X_SCHEME = "x-forwarded-proto";
	
	public static final String HEADER_X_CONTEXT_PATH = "x-forwarded-prefix";
	
	public static final String HEADER_X_IP = "x-forwarded-for";
	
	public static final String UNKNOWN = "unknown";
	
	public static final String HEADER_PROXY_CLIENT_IP = "Proxy-Client-IP";
	
	public static final String HEADER_WL_PROXY_CLIENT_IP = "WL-Proxy-Client-IP";
	
	private static final String SEPRATOR = ",";
	
	public static String getFullPath(HttpServletRequest request) {
		return getFullPath(request, StringUtils.EMPTY);
	}
	
	public static String getFullPath(HttpServletRequest request, String uri) {
		String scheme = getScheme(request);
		String host = getHost(request);
		String contextPath = getContextPath(request) ;
		StringBuilder builder = new StringBuilder();
		builder.append(scheme).append("://").append(host);
		builder.append(contextPath);
		builder.append(uri);
		return  builder.toString();
	}
	
	public static String getScheme(HttpServletRequest request) {
		String scheme = ObjectUtils.defaultIfNull(request.getHeader(HEADER_X_SCHEME), request.getScheme());
		if(StringUtils.contains(scheme, SEPRATOR)) {
			scheme = StringUtils.split(scheme, SEPRATOR)[0];
		}
		return scheme;
	}
	
	public static String getHost(HttpServletRequest request) {
		String host = ObjectUtils.defaultIfNull(request.getHeader(HEADER_X_HOST), 
				StringUtils.join(request.getServerName(), ":", getPort(request)));
		if(StringUtils.contains(host, SEPRATOR)) {
			host = StringUtils.split(host, SEPRATOR)[0];
		}
		return host;
	}
	
	public static String getPort(HttpServletRequest request) {
		String port = ObjectUtils.defaultIfNull(request.getHeader(HEADER_X_PORT), String.valueOf(request.getServerPort()));
		if(StringUtils.contains(port, SEPRATOR)) {
			port = StringUtils.split(port, SEPRATOR)[0];
		}
		return port;
	}
	
	public static String getContextPath(HttpServletRequest request) {
		return ObjectUtils.defaultIfNull(request.getHeader(HEADER_X_CONTEXT_PATH), request.getContextPath());
	}
	
	/**
	 * 获取ip
	 * @param request
	 * @return
	 */
	public static String getIpAddr(HttpServletRequest request) {
		String ip = request.getHeader(HEADER_X_IP);
		if(StringUtils.isBlank(ip) || StringUtils.equalsIgnoreCase(UNKNOWN, ip)) {
			ip = request.getHeader(HEADER_PROXY_CLIENT_IP);
		}
		if(StringUtils.isBlank(ip) || StringUtils.equalsIgnoreCase(UNKNOWN, ip)) {
			ip = request.getHeader(HEADER_WL_PROXY_CLIENT_IP);
		}
		if(StringUtils.isBlank(ip) || StringUtils.equalsIgnoreCase(UNKNOWN, ip)) {
			ip = request.getRemoteAddr();
		}
		if(StringUtils.equals(ip, "0:0:0:0:0:0:0:1")) {
			ip = "127.0.0.1";
		}
		if(StringUtils.contains(ip, SEPRATOR)) {
			ip = StringUtils.split(ip, SEPRATOR)[0];
		}
		return ip;
	}
	
	private HttpServletRequestUtils() {}
	
}
